package com.gs.cd.service.old.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gs.cd.api.ApiResult;
import com.gs.cd.model.resource.SysResource;
import com.gs.cd.model.resource.SysResourceGroup;
import com.gs.cd.service.old.controller.model.ResourceGroup;
import com.gs.cd.service.resource.api.ResourceApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 资源组
 *
 * @author hippo
 * @since 2020-02-12
 */
@RestController
@RequestMapping("/auth/resource/group")
public class ResourceGroupController {
    @Autowired
    ResourceApi resourceApi;

    /**
     * 查询资源
     *
     * @param tenantCode
     * @return
     */
    @ResponseBody
    @GetMapping("list/{tenantCode}")
    public ApiResult list(@PathVariable("tenantCode") @Validated @NotEmpty(message = "租户编码不能为空") String tenantCode) {
        List<SysResourceGroup> list = resourceApi.listRootResourceGroup();
        Set<String> collect = list.stream().map(SysResourceGroup::getId).collect(Collectors.toSet());
        List<SysResourceGroup> list1 = resourceApi.resourceByGroupIds(collect);
        ArrayList<ResourceGroup> resourceGroups = new ArrayList<>();
        list1.forEach(l->{
            ResourceGroup parse = ResourceGroup.parse(l);
            if (l.getSysResourceList()!=null){
                parse.setResourceList(l.getSysResourceList().stream().map(SysResource::getId).collect(Collectors.toList()));
            }
            resourceGroups.add(parse);
        });
        return ApiResult.success(resourceGroups);
    }
}

